import 'dart:convert' show json;

import 'package:loxen/mode/address_mode.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class OperationList {
  OperationList({
    this.count,
    this.next,
    this.previous,
    this.results,
  });

  factory OperationList.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<Operation> results = jsonRes['results'] is List ? <Operation>[] : null;
    if (results != null) {
      for (final dynamic item in jsonRes['results']) {
        if (item != null) {
          results.add(Operation.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return OperationList(
      count: asT<int>(jsonRes['count']),
      next: asT<String>(jsonRes['next']),
      previous: asT<String>(jsonRes['previous']),
      results: results,
    );
  }

  int count;
  String next;
  String previous;
  List<Operation> results;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'count': count,
        'next': next,
        'previous': previous,
        'results': results,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Operation {
  Operation({
    this.id,
    this.name,
    this.area,
    this.address,
    this.lng,
    this.lat,
    this.mobile,
    this.support_city,
    this.support_shop,
    this.support_express,
    this.add_time,
    this.update_time,
    this.city_range,
    this.express_range,
  });

  factory Operation.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<CityRange> city_range = jsonRes['city_range'] is List ? <CityRange>[] : null;
    if (city_range != null) {
      for (final dynamic item in jsonRes['city_range']) {
        if (item != null) {
          city_range.add(CityRange.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    final List<Object> express_range = jsonRes['express_range'] is List ? <Object>[] : null;
    if (express_range != null) {
      for (final dynamic item in jsonRes['express_range']) {
        if (item != null) {
          express_range.add(asT<Object>(item));
        }
      }
    }
    return Operation(
      id: asT<int>(jsonRes['id']),
      name: asT<String>(jsonRes['name']),
      area: Area.fromJson(asT<Map<String, dynamic>>(jsonRes['area'])),
      address: asT<String>(jsonRes['address']),
      lng: asT<String>(jsonRes['lng']),
      lat: asT<String>(jsonRes['lat']),
      mobile: asT<String>(jsonRes['mobile']),
      support_city: asT<bool>(jsonRes['support_city']),
      support_shop: asT<bool>(jsonRes['support_shop']),
      support_express: asT<bool>(jsonRes['support_express']),
      add_time: asT<String>(jsonRes['add_time']),
      update_time: asT<String>(jsonRes['update_time']),
      city_range: city_range,
      express_range: express_range,
    );
  }

  double size;
  int id;
  String name;
  Area area;
  String address;
  String lng;
  String lat;
  String mobile;
  bool support_city;
  bool support_shop;
  bool support_express;
  String add_time;
  String update_time;
  List<CityRange> city_range;
  List<Object> express_range;

  String getCityString() {
    return '${area.province} ${area.city} ${area.district}';
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'area': area,
        'address': address,
        'lng': lng,
        'lat': lat,
        'mobile': mobile,
        'support_city': support_city,
        'support_shop': support_shop,
        'support_express': support_express,
        'add_time': add_time,
        'update_time': update_time,
        'city_range': city_range,
        'express_range': express_range,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class CityRange {
  CityRange({
    this.province,
    this.city,
    this.district,
  });

  factory CityRange.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : CityRange(
          province: asT<String>(jsonRes['province']),
          city: asT<String>(jsonRes['city']),
          district: asT<String>(jsonRes['district']),
        );

  String province;
  String city;
  String district;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'province': province,
        'city': city,
        'district': district,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
